package com.snow.controller.admin;

import com.snow.constant.Types;
import com.snow.controller.BaseController;
import com.snow.pojo.dto.cond.MetaCond;
import com.snow.pojo.model.MetaDomain;
import com.snow.service.meta.MetaService;
import com.snow.utils.ApiResponse;
import com.snow.utils.Commons;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.internal.dynalink.linker.LinkerServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @author: snow
 * @Date: 2020-01-31
 **/
@Api("友情链接")
@Controller
@RequestMapping("/admin/links")
public class LinksController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(LinksController.class);
    @Autowired
    private MetaService metaService;
    @Autowired
    private Commons commons;

    @ApiOperation("进入链接页面")
    @GetMapping("")
    public String LinksIndex(Model model){

        MetaCond cond = new MetaCond();
        cond.setType(Types.LINK.getType());
        List<MetaDomain> links = metaService.getMetasByCond(cond);
        model.addAttribute("links",links);
        model.addAttribute("commons",commons);
        return "admin/links";
    }
    @ApiOperation("新增链接")
    @PostMapping("/save")
    @ResponseBody
    public ApiResponse addLinks(
            @ApiParam(name="title",value="标题",required = true)
            @RequestParam(name="titile",required = true)
            String title,
            @ApiParam(name="url",value="url",required = true)
            @RequestParam(name="url",required = true)
            String url,
            @ApiParam(name="logo",value = "logo",required = false)
            @RequestParam(name="logo",required = false)
            String logo,
            @ApiParam(name="mid",value = "mid",required = false)
            @RequestParam(name="mid",required = false)
            Integer mid,
            @ApiParam(name="sort",value="sort",required = false)
            @RequestParam(name="sort",required = false,defaultValue = "0")
            Integer sort
    ){
        try{
            MetaDomain domain = new MetaDomain();
            domain.setName(title);
            domain.setType(Types.LINK.getType());
            domain.setSlug(url);
            domain.setSort(sort);
            domain.setDescription(logo);

            metaService.addMeta(domain);

        }catch (Exception e){
            e.printStackTrace();
            logger.error("链接添加失败！");
            return ApiResponse.fail("添加失败！");
        }
        return ApiResponse.success();
    }
    @ApiOperation("删除链接")
    @PostMapping("/delete")
    @ResponseBody
    public ApiResponse deleteLink(
            @ApiParam(name="mid",value = "mid",required = true)
            @RequestParam(name="mid",required = true)
            Integer mid
    ){
        try{
            metaService.deleteMetaById(mid);

        }catch (Exception e){
            e.printStackTrace();

        }
        return ApiResponse.success();
    }


}
